package org.wattdepot.cli.commands;

import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;

/**
 * This class tests the power chart command of the 
 * CommandLineInterface's actual commands.
 * @author David Mau
 */
public class TestCommandPowerChart {
  // Variables to have PMD stop complaining.
  private String generated = "generated";
  private String consumed = "consumed";
  private String novSix = "2009-11-06";
  
  /**
   * Test normal operation of the power chart command.
   */
  
  @Test
  public void testPowerChart() {
    // Create a new power chart command object.
    PowerChartCommand command = new PowerChartCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( 
        generated, "SIM_WAIAU_5", novSix, "2009-11-10", "30", "test.html" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Ready the output that we know is true
    String knownOutput = "Saved to file.";
    
    // Compare the two to see if they are the same.
    int result = sourceOutput.compareTo(knownOutput);
    assertSame("Checking safe execution of power chart command", result, 0);
  }

  /**
   * Test bad time stamp detection for the power chart command.
   */
  @Test
  public void testPowerChartBadTime() {
    // Create a new total sampling command object.
    PowerChartCommand command = new PowerChartCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( 
        consumed, "SIM_WAIAU_1", "SOMEDAY", "2009-11-09", "10", "one.html" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a bad timestamp", sourceOutput
        .contains(WattDepotCliCommand.badTimeStamp));

    // Create a new list to store the args
    args = Arrays.asList( 
        generated, "SIM_WAIAU_1", "2009-11-08", "TOMORROW", "15", "two.html" );
    
    // Get the output from the command
    sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a bad timestamp", sourceOutput
        .contains(WattDepotCliCommand.badTimeStamp));
  }

  
  /**
   * Test bad source detection for the power chart command.
   */
  @Test
  public void testPowerChartBadSource() {
    // Create a new power chart command object.
    PowerChartCommand command = new PowerChartCommand(null);
    
    // Attempt to use a bad source with carbon
    // Create a new List to store the args
    List<String> args = Arrays.asList( 
        consumed, "SUN", novSix, "2009-11-07", "20", "three.html" );
        
   // Get the output from the command
   String sourceOutput = command.doCommand( args );
        
   // Check to see if the correct error returned
   assertTrue("Checking for correct response to a bad source",
       sourceOutput.contains(WattDepotCliCommand.resourceNotFound));
  }

  /**
   * Test bad option given for generated or consumed for the power chart command.
   */
  @Test
  public void testPowerChartBadOption() {
    // Create a new total sampling command object.
    PowerChartCommand command = new PowerChartCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( 
        "black hole", "SIM_OAHU", "2009-11-04", "2009-11-05", "25", "four.html" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a bad option",
        sourceOutput.contains(WattDepotCliCommand.generatedOrConsumed));
  }


  /**
   * Test a bad interval given for the power chart command.
   * The given String should be convertible into an Integer.
   */
  @Test
  public void testPowerChartBadInterval() {
    // Attempt to use a bad interval
    // Create a new power chart command object.
    PowerChartCommand command = new PowerChartCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( 
        generated, "SIM_WAIAU", "2009-11-02", "2009-11-03", "BEES", "five.html" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a bad interval", sourceOutput
        .contains(WattDepotCliCommand.intervalNotNumber));
  }

  /**
   * Test another bad interval given for the power chart command. 
   * The given String should be a non-zero, positive integer.
   */
  @Test
  public void testPowerChartBadIntervalInteger() {
    // Create a new total sampling command object.
    PowerChartCommand command = new PowerChartCommand(null);
    
    // Attempt to use a negative interval
    // Create a new List to store the args
    List<String> args = Arrays.asList( 
        consumed, "SIM_AES", "2009-11-02", "2009-11-03", "-1", "six.html" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a negative interval", sourceOutput
        .contains(WattDepotCliCommand.intervalNegativeOrZero));
    
    // Attempt to use zero interval
    // Create a new List to store the args
    args = Arrays.asList( 
        generated, "SIM_OAHU", "2009-11-03", "2009-11-04", "0", "seven.html" );
    
    // Get the output from the command
    sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a zero interval", sourceOutput
        .contains(WattDepotCliCommand.intervalNegativeOrZero));
  }  

  /**
   * Test a bad file given for the power chart command.
   */
  @Test
  public void testPowerChartBadFile() {
    // Create a new power chart command object.
    PowerChartCommand command = new PowerChartCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( 
        generated, "SIM_WAIAU_5", novSix, "2009-11-10", "30", "." );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a bad file", 
        sourceOutput.contains(WattDepotCliCommand.badFile));
  }
  
  /**
   * Test a bad set of start and end dates given for the power chart command.
   */
  @Test
  public void testPowerChartBadStartAndEndDates() {
    // Create a new power chart command object.
    PowerChartCommand command = new PowerChartCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( 
        generated, "SIM_HONOLULU", "2009-11-07", novSix, "120", "eight.html" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a bad set of start and end dates", 
        sourceOutput.contains(WattDepotCliCommand.endDateBeforeStartDate));
  }
  
  /**
   * Tests to see if the power chart command can handle excess arguments.
   */
  @Test
  public void testExcessArguments() {
    // Create a new power chart command object.
    PowerChartCommand command = new PowerChartCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList("1", "2", "3", "4", "5", "6", "7");
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Make sure that the command is returning the error properly
    assertTrue("Checking excess power chart argument handling", 
        sourceOutput.contains(WattDepotCliCommand.tooManyArguments));
  }
  
  /**
   * Tests to see if the power chart command can handle not enough arguments.
   */
  @Test
  public void testFewArguments() {
    // Create a new power chart command object.
    PowerChartCommand command = new PowerChartCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList("1");
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Make sure that the command is returning the error properly
    assertTrue("Checking power chart handling not enough arguments", 
        sourceOutput.contains(WattDepotCliCommand.missingArguments));
  }
}
